﻿//********************************************************************************//
//创建日期：<创建日期，2010-08-02>
//创建作者：<王一飞，wangyifei@shanghai3h.com>
//功能说明：S_SETTINGS实体层次
//********************************************************************************//

#region Entity

using System;
using System.Data;
using Entity;
using Oracle.DataAccess.Client;
using Platform.Configuration;
using Platform.Data;

namespace Entity
{
    [Serializable]
    public class S_SETTINGS
    {
        private decimal m_dec_id;

        private string m_str_configvalue;

        private string m_str_keyname;

        private string m_str_remark;

        /// <summary>
        ///     自增量ID
        /// </summary>
        public decimal ID
        {
            get { return m_dec_id; }
            set { m_dec_id = value; }
        }

        /// <summary>
        ///     键名
        /// </summary>
        public string KEYNAME
        {
            get { return m_str_keyname; }
            set { m_str_keyname = value; }
        }

        /// <summary>
        ///     键值
        /// </summary>
        public string CONFIGVALUE
        {
            get { return m_str_configvalue; }
            set { m_str_configvalue = value; }
        }

        /// <summary>
        ///     备注
        /// </summary>
        public string REMARK
        {
            get { return m_str_remark; }
            set { m_str_remark = value; }
        }
    }
}

#endregion

#region EntityProcess

namespace BLL.Access
{
    //using System.Data.OracleClient;

    public class S_SETTINGSProcess
    {
        #region Field

        protected string conn;

        #endregion

        #region Construction

        public S_SETTINGSProcess()
        {
            conn = ConfigHelper.PlatformConnString;
        }

        #endregion

        #region public

        /// <summary>
        ///     根据S_SETTINGS实体添加或者修改S_SETTINGS信息
        /// </summary>
        public bool InsertOrUpdate(S_SETTINGS S_SETTINGS, OracleTransaction Transaction)
        {
            if (S_SETTINGS.ID == 0)
                InsertS_SETTING(S_SETTINGS, Transaction);
            else
                UpdateS_SETTING(S_SETTINGS, Transaction);
            return S_SETTINGS.ID == 0 ? false : true;
        }

        /// <summary>
        ///     根据S_SETTINGS实体添加或修改S_SETTINGS信息
        /// </summary>
        public bool InsertOrUpdate(S_SETTINGS S_SETTINGS)
        {
            return InsertOrUpdate(S_SETTINGS, null);
        }


        /// <summary>
        ///     根据S_SETTINGS主键删除S_SETTINGS信息
        /// </summary>
        /// <param name="ID"></param>
        public bool DeleteS_SETTING(decimal ID, OracleTransaction Transaction)
        {
            var spName = "CS_DelS_SETTING";
            var storedParams = OracleHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = ID;
            var result = 0;
            if (Transaction == null)
                result = OracleHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                result = OracleHelper.ExecuteNonQuery(Transaction, CommandType.StoredProcedure, spName, storedParams);
            return result == 0 ? false : true;
        }

        /// <summary>
        ///     根据S_SETTINGS主键删除S_SETTINGS信息
        /// </summary>
        /// <param name="ID"></param>
        public bool DeleteS_SETTING(decimal ID)
        {
            return DeleteS_SETTING(ID, null);
        }

        /// <summary>
        ///     根据条件删除S_SETTINGS信息
        /// </summary>
        /// <param name="WhereClause">条件子句(Oracle语句)不带where</param>
        /// <param name="Transaction">事务，如果为null,则不做事务</param>
        /// <returns>如果删除成功，返回true,否则返回fals</returns>
        public bool DeleteS_SETTINGsDynamic(string WhereClause, OracleTransaction Transaction)
        {
            var spName = "CS_DelS_SETTINGsDyn";
            var storedParams = OracleHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = WhereClause;
            var result = 0;
            if (Transaction == null)
                result = OracleHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                result = OracleHelper.ExecuteNonQuery(Transaction, CommandType.StoredProcedure, spName, storedParams);
            return result == 0 ? false : true;
        }

        /// <summary>
        ///     根据条件删除S_SETTINGS信息
        /// </summary>
        /// <param name="WhereClause">条件子句(Oracle语句)不带where</param>
        /// <param name="Transaction">事务，如果为null,则不做事务</param>
        /// <returns>如果删除成功，返回true,否则返回fals</returns>
        public bool DeleteS_SETTINGsDynamic(string WhereClause)
        {
            return DeleteS_SETTINGsDynamic(WhereClause, null);
        }

        /// <summary>
        ///     根据S_SETTINGS主键选择S_SETTINGS信息
        /// </summary>
        /// <param name="Transaction">事务，如果为null,则不做事务</param>
        /// <returns>S_SETTINGS信息</returns>
        public S_SETTINGS SelectS_SETTING(decimal ID, OracleTransaction Transaction)
        {
            var spName = "CS_SelS_SETTING";
            var storedParams = OracleHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = ID;
            OracleDataReader rd;
            if (Transaction == null)
                rd = OracleHelper.ExecuteReader(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                rd = OracleHelper.ExecuteReader(Transaction, CommandType.StoredProcedure, spName, storedParams);
            if (rd.Read())
            {
                var S_SETTINGS = PopulateS_SETTINGS(rd);
                rd.Close();
                return S_SETTINGS;
            }
            return null;
        }

        /// <summary>
        ///     根据S_SETTINGS主键选择S_SETTINGS信息
        /// </summary>
        /// <returns>S_SETTINGS信息</returns>
        public S_SETTINGS SelectS_SETTING(decimal ID)
        {
            return SelectS_SETTING(ID, null);
        }

        /// <summary>
        ///     选择所有S_SETTINGS信息
        /// </summary>
        /// <param name="Transaction">事务，如果为null,则不做事务</param>
        /// <returns>所有S_SETTINGS信息</returns>
        public DataTable SelectS_SETTINGsAll(OracleTransaction Transaction)
        {
            var spName = "CS_SelS_SETTINGsAll";
            var storedParams = OracleHelperParameterCache.GetSpParameterSet(conn, spName);
            DataSet ds = null;
            if (Transaction == null)
                ds = OracleHelper.ExecuteDataset(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                ds = OracleHelper.ExecuteDataset(Transaction, CommandType.StoredProcedure, spName, storedParams);
            if (ds != null && ds.Tables.Count > 0)
            {
                AddRowNumber(ds.Tables[0]);
                return ds.Tables[0];
            }
            return null;
        }

        /// <summary>
        ///     选择所有S_SETTINGS信息
        /// </summary>
        /// <returns>所有S_SETTINGS信息</returns>
        public DataTable SelectS_SETTINGsAll()
        {
            return SelectS_SETTINGsAll(null);
        }

        /// <summary>
        ///     根据条件选择S_SETTINGS信息
        /// </summary>
        /// <param name="WhereClause">条件子句(不带where)</param>
        /// <param name="OrderByExpression">排序子句</param>
        /// <param name="Transaction">事务，如果为null,则不做事务</param>
        /// <returns>S_SETTINGS信息</returns>
        public DataTable SelectS_SETTINGsDynamic(string WhereClause, string OrderByExpression,
            OracleTransaction Transaction)
        {
            var spName = "CS_SelS_SETTINGsDyn";
            var storedParams = OracleHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = string.IsNullOrEmpty(WhereClause.Trim()) ? "1=1" : WhereClause;
            storedParams[1].Value = OrderByExpression;
            DataSet ds = null;
            if (Transaction == null)
                ds = OracleHelper.ExecuteDataset(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                ds = OracleHelper.ExecuteDataset(Transaction, CommandType.StoredProcedure, spName, storedParams);
            if (ds != null && ds.Tables.Count > 0)
            {
                AddRowNumber(ds.Tables[0]);
                return ds.Tables[0];
            }
            return null;
        }


        /// <summary>
        ///     根据条件选择S_SETTINGS信息
        /// </summary>
        /// <param name="WhereClause">条件子句(不带where)</param>
        /// <param name="OrderByExpression">排序子句</param>
        /// <returns>S_SETTINGS信息</returns>
        public DataTable SelectS_SETTINGsDynamic(string WhereClause, string OrderByExpression)
        {
            return SelectS_SETTINGsDynamic(WhereClause, OrderByExpression, null);
        }


        /// <summary>
        ///     根据条件选择S_SETTINGS信息
        /// </summary>
        /// <param name="WhereClause">条件子句(不带where)</param>
        /// <param name="OrderByExpression">排序子句</param>
        /// <param name="PageIndex">页码</param>
        /// <param name="PageSize">页数</param>
        /// <param name="Transaction">事务，如果为null,则不做事务</param>
        /// <returns>S_SETTINGS信息</returns>
        public DataTable SelectS_SETTINGsDynamic(string WhereClause, string OrderByExpression, int PageIndex,
            int PageSize, OracleTransaction Transaction)
        {
            var spName = "CS_SelS_SETTINGsDynPaged";
            var storedParams = OracleHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = string.IsNullOrEmpty(WhereClause.Trim()) ? "1=1" : WhereClause;
            storedParams[1].Value = OrderByExpression;
            storedParams[2].Value = PageIndex;
            storedParams[3].Value = PageSize;
            DataSet ds = null;
            if (Transaction == null)
                ds = OracleHelper.ExecuteDataset(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                ds = OracleHelper.ExecuteDataset(Transaction, CommandType.StoredProcedure, spName, storedParams);
            if (ds != null && ds.Tables.Count > 0)
            {
                AddRowNumber(ds.Tables[0]);
                return ds.Tables[0];
            }
            return null;
        }


        /// <summary>
        ///     根据条件选择S_SETTINGS信息
        /// </summary>
        /// <param name="WhereClause">条件子句(不带where)</param>
        /// <param name="OrderByExpression">排序子句</param>
        /// <param name="PageIndex">页码</param>
        /// <param name="PageSize">页数</param>
        /// <returns>S_SETTINGS信息</returns>
        public DataTable SelectS_SETTINGsDynamic(string WhereClause, string OrderByExpression, int PageIndex,
            int PageSize)
        {
            return SelectS_SETTINGsDynamic(WhereClause, OrderByExpression, PageIndex, PageSize, null);
        }

        /// <summary>
        ///     根据条件更新S_SETTINGS字段
        /// </summary>
        /// <returns>受影响行数</returns>
        public int UpdateS_SETTINGsDynamic(string UpdateValue, string WhereClause, OracleTransaction Transaction)
        {
            var result = 0;
            if (UpdateValue == "" || WhereClause == "")
                return 0;
            var spName = "CS_UpdS_SETTINGDyn";
            var storedParams = OracleHelperParameterCache.GetSpParameterSet(conn, spName);
            DataSet ds = null;
            storedParams[0].Value = UpdateValue;
            storedParams[1].Value = WhereClause;
            if (Transaction == null)
                ds = OracleHelper.ExecuteDataset(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                ds = OracleHelper.ExecuteDataset(Transaction, CommandType.StoredProcedure, spName, storedParams);
            result = Convert.ToInt32(storedParams[2].Value);
            return result;
        }

        /// <summary>
        ///     根据条件更新S_SETTINGS字段
        /// </summary>
        /// <returns>受影响行数</returns>
        public int UpdateS_SETTINGsDynamic(string UpdateValue, string WhereClause)
        {
            return UpdateS_SETTINGsDynamic(UpdateValue, WhereClause, null);
        }

        #endregion

        #region Private

        private int InsertS_SETTING(S_SETTINGS S_SETTINGS, OracleTransaction Transaction)
        {
            var spName = "CS_InsS_SETTING";
            var storedParams = OracleHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = S_SETTINGS.KEYNAME;
            storedParams[1].Value = S_SETTINGS.CONFIGVALUE;
            storedParams[2].Value = S_SETTINGS.REMARK;
            var result = 0;
            if (Transaction == null)
                result = OracleHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                result = OracleHelper.ExecuteNonQuery(Transaction, CommandType.StoredProcedure, spName, storedParams);
            S_SETTINGS.ID = Convert.ToInt32(storedParams[3].Value);
            return result;
        }

        private bool UpdateS_SETTING(S_SETTINGS S_SETTINGS, OracleTransaction Transaction)
        {
            var spName = "CS_UpdS_SETTING";
            var storedParams = OracleHelperParameterCache.GetSpParameterSet(conn, spName);
            storedParams[0].Value = S_SETTINGS.KEYNAME;
            storedParams[1].Value = S_SETTINGS.CONFIGVALUE;
            storedParams[2].Value = S_SETTINGS.REMARK;
            storedParams[3].Value = S_SETTINGS.ID;
            var result = 0;
            if (Transaction == null)
                result = OracleHelper.ExecuteNonQuery(conn, CommandType.StoredProcedure, spName, storedParams);
            else
                result = OracleHelper.ExecuteNonQuery(Transaction, CommandType.StoredProcedure, spName, storedParams);
            return result == 0 ? false : true;
        }


        private S_SETTINGS PopulateS_SETTINGS(IDataReader dr)
        {
            var S_SETTINGS = new S_SETTINGS();
            S_SETTINGS.ID = dr["ID"] == DBNull.Value ? 0 : (decimal) dr["ID"];
            S_SETTINGS.KEYNAME = dr["KEYNAME"] == DBNull.Value ? null : (string) dr["KEYNAME"];
            S_SETTINGS.CONFIGVALUE = dr["CONFIGVALUE"] == DBNull.Value ? null : (string) dr["CONFIGVALUE"];
            S_SETTINGS.REMARK = dr["REMARK"] == DBNull.Value ? null : (string) dr["REMARK"];
            return S_SETTINGS;
        }


        private void AddRowNumber(DataTable dt)
        {
            dt.Columns.Add(new DataColumn("RowNumber", typeof(int)));
            for (var i = 0; i < dt.Rows.Count; i++)
                dt.Rows[i][dt.Columns.Count - 1] = i + 1;
        }

        #endregion
    }
}

#endregion